package com.zhongkai.web.control.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.zhongkai.json.JSONObject;
import com.zhongkai.model.book.TDjCcdjxx;
import com.zhongkai.model.code.TCsXtcs;
import com.zhongkai.model.config.User;
import com.zhongkai.model.declare.TSbCcsspxx;
import com.zhongkai.service.declare.SpCancelService;

@Controller
@RequestMapping(value="/system/ajax/spCancel.do")
public class spCancelAjaxControl {
	
	private SpCancelService spCancelService;
	private Logger log=Logger.getLogger(this.getClass());
	
	@Resource
	public void setSpCancelService(SpCancelService spCancelService) {
		this.spCancelService = spCancelService;
	}
	
	

	/*根据税票号码检验,Ajax调用*/
	@RequestMapping(params="method=check")
	public String spCancel(HttpServletResponse response,HttpServletRequest request,String dqsphm,String dqzb) throws Exception{
			PrintWriter out=response.getWriter();
			
			User user=(User)request.getSession().getAttribute("user");
			//取得当前登录用户的编号
			String identifier=user.getUserIdentifier();
			//根据当前用户编号取得系统参数
			TCsXtcs tCsXtcs=(TCsXtcs)spCancelService.getSingle(TCsXtcs.class, "csz", identifier);
			//取得当前票号的票税信息
			//TSbCcsspxx tSbCcsspxx=(TSbCcsspxx)spCancelService.getSingle(TSbCcsspxx.class, "sphm", dqsphm);
			List<TSbCcsspxx> tSbCcsspxxList=(List<TSbCcsspxx>)spCancelService.select("from TSbCcsspxx where sphm=? and zb=? and pzzlDm=?", new Object[]{dqsphm,dqzb,"01"});
			TSbCcsspxx tSbCcsspxx=tSbCcsspxxList.get(0);
			if(tSbCcsspxx==null){
				//out.write("<script>alert('无税票信息!');</script>");
				out.write("无税票信息!");
				return null;
			}
			if(!"02".equals(tSbCcsspxx.getPzztDm())){
				//out.write("<script>alert('该税票已作废或者为空白!');</script>");
				out.write("该税票已作废或者为空白!");
				return null;
			}
			if(tSbCcsspxx.getJbXh()!=null){
				//out.write("<script>alert('此税票已结报,不能作废!');</script>");
				out.write("此税票已结报,不能作废!");
				return null;
			}
			if(!tSbCcsspxx.getCzrDm().equals(identifier)&&!"0002".equals(tCsXtcs.getCsDm())){
				//out.write("<script>alert('您不能操作此税票!');</script>");
				out.write("您不能操作此税票!");
				return null;
			}
			out.write("ok");
			return null;
	}
	
	
	
}
